define(["jquery","utils","DataManager","HeatMapMouseClickArea","HeatMapKeyPressedArea","HeatMapCharAnalysArea","HeatMapMouseMoveArea","HeatMapAdminInterface"], function($, utils,DataManager,HeatMapMouseClickArea,HeatMapKeyPressedArea,HeatMapCharAnalysArea,HeatMapMouseMoveArea,HeatMapAdminInterface){

    function HeatMapMaster(){
        var t = this;

        t.dataManager = new DataManager();
        t.adminMainInterface = new HeatMapAdminInterface(t.dataManager);
        t.heatMapAreasMouseClickList = new Array();
        t.heatMapAreasMouseMoveList  = new Array();
        t.heatMapAreasKeyPressedList  = new Array();
        t.heatmapAreaCharAnalysList = new Array();
        //#region INITFUNCTIONS
            
            t.handleFileSelect = function(evt){

                var files = evt.target.files; // FileList object
                t.adminMainInterface.clearVisualizer();
                // Loop through the FileList and render image files as thumbnails.
                var reader = new FileReader();
                for (var i = 0, f; f = files[i]; i++) {

                    reader.onload = function(e) {
                        // Print the contents of the file
                        var text = e.target.result;

                        var lines = text.split(/[\r\n]+/g); // tolerate both Windows and Unix linebreaks

                        for(var i = 0; i < lines.length; i++){

                            t.dataManager.loadDataIntoElement(lines[i],t.getMatchingDomObj(lines[i]));
                       }
                    };
                    reader.readAsText(f,"UTF-8");
                }
                window.alert("Fertisch");
            };
            t.getMatchingDomObj= function(xLine){
                var pID =  xLine.split(";")[0];
                var pHeatType = parseInt(xLine.split(";")[1]);
               
                switch(pHeatType){
                    case 1:
                        return t.checkListForMatchingDomObj(pID,t.heatMapAreasMouseClickList);
                    case 2:
                        return t.checkListForMatchingDomObj(pID,t.heatMapAreasMouseMoveList);
                    case 3:
                        return t.checkListForMatchingDomObj(pID,t.heatmapAreaCharAnalysList);
                    case 4:
                        return t.checkListForMatchingDomObj(pID,t.heatMapAreasKeyPressedList);
                    default:
                        return null;
                }
            };
            t.checkListForMatchingDomObj= function(xID,xList){

                return xList.filter(function( obj ) {
                    return obj.domObj.id == xID;
                })[0];
            };
            t.initAllMouseClickAreas = function(){
                t.heatMapAreasMouseClickList = [];

                $('.heatmapAreaClick').each(function(index,obj){
                    
                    var pClickArea = new HeatMapMouseClickArea(obj);
                    pClickArea.initInputCaptureAbility();

                    if(utils.showAdminInterface){
                        
                        pClickArea.init(null,25);
                        pClickArea.positionHeatmap();

                    }

                    t.heatMapAreasMouseClickList.push(pClickArea);

                });
            };
            t.initAllMouseMoveAreas = function(){
                t.heatMapAreasMouseMoveList = [];

                $('.heatmapAreaMouseMove').each(function(index,obj){
                    
                    var pMoveArea = new HeatMapMouseMoveArea(obj);
                    pMoveArea.initInputCaptureAbility();
                    
                    if(utils.showAdminInterface){
                        pMoveArea.init();
                        pMoveArea.positionHeatmap();
                    }

                    t.heatMapAreasMouseMoveList.push(pMoveArea);

                });
            };
            t.initAllKeyPressedAreas = function(){
                t.heatMapAreasKeyPressedList = [];

                $('.heatmapAreaKeyPressed').each(function(index,obj){

                    var pKeyPressedArea = new HeatMapKeyPressedArea(obj);
                    
                    pKeyPressedArea.initInputCaptureAbility();

                    if(utils.showAdminInterface){
                        pKeyPressedArea.init(t.adminMainInterface.visualizerKeyPressed.getHeatmap());
                        pKeyPressedArea.positionHeatmap();
                    }

                    t.heatMapAreasKeyPressedList.push(pKeyPressedArea);

                });
            };
            t.initAllCharAnalysAreas = function(){
                t.heatmapAreaCharAnalysList = [];

                $('.heatmapAreaCharAnalyse').each(function(index,obj){
                    
                    var pAnalyseArea = new HeatMapCharAnalysArea(obj);
                    
                    pAnalyseArea.initInputCaptureAbility();
                                        
                    if(utils.showAdminInterface){
                        pAnalyseArea.init(t.adminMainInterface.visualizerCharAnalysis.getHeatmap());
                        pAnalyseArea.positionHeatmap();
                    }
                    
                    t.heatmapAreaCharAnalysList.push(pAnalyseArea);

                });
            };
            t.initFileHandling = function (){

            };
            t.initMasterMind = function(){
                
                if(utils.showAdminInterface){
                    
                    t.adminMainInterface.init();
                    t.adminMainInterface.initLoadDataButton(t.handleFileSelect);    

                }
                
                t.initAllMouseMoveAreas();
                t.initAllKeyPressedAreas();
                t.initAllCharAnalysAreas();
                t.initAllMouseClickAreas(); 

                if(utils.showAdminInterface){
                    
                    t.adminMainInterface.initAreaListInterfaceMouseClick(t.heatMapAreasMouseClickList);
                    t.adminMainInterface.initAreaListInterfaceMouseMove(t.heatMapAreasMouseMoveList);
                    t.adminMainInterface.initAreaListInterfaceKeyPressed(t.heatMapAreasKeyPressedList);
                    t.adminMainInterface.initAreaListInterfaceCharAnalysis(t.heatmapAreaCharAnalysList);

                }
              
            };
            window.onbeforeunload = function(e) {
                t.dataManager.startDownload(t.heatMapAreasMouseClickList,
                                            t.heatMapAreasMouseMoveList,
                                            t.heatMapAreasKeyPressedList,
                                            t.heatmapAreaCharAnalysList);
            return true;
            };
        //#endregion

    }
    return HeatMapMaster;
});
//#endregion